import router  from '@/router'
import {post}  from '@/util/request'
import storage from '@/util/storage'
import {getAddress} from '@/plugin/Map/Mapaddress'

export default {
	namespace:'home',
	namespaced:true,
	state:{
		isDriverLoading:false,
		driverList:[],
		rundriverList:[],
		provinceList:[],
		cityList:[],
		plList:[],
		alarmList:[],
		overSpeedList:[],
		listLoading:false,
		trackList:[]
	},
	mutations:{
		changeIsDriverLoading(state,isLoading){
			state.isDriverLoading = isLoading;
		},
		changelistLoading(state,isLoading){
			state.listLoading = isLoading;
		},
		changeDriverList(state,v){
			state.driverList = v;
		},
		changerunDriverList(state,v){
			state.rundriverList = v;
		},
		changeProvinceList(state,v){
			state.provinceList = v;
		},
		changeCityList(state,v){
			state.cityList = v;
		},
		changeoverSpeedList(state,v){
			state.overSpeedList = v;
		},
		changeplList(state,v){
			state.plList = v;
		},
		changealarmList(state,v){
			state.alarmList = v;
		},
		changetrackList(state,v){
			state.trackList = v;
		}
	},
	actions:{
		loadTrack({commit,dispatch},form){
			return post('/monitor/rt/findTodayTrack',form);
		},
		async loadDriverList({commit,dispatch}){
			commit('changeIsDriverLoading',true);
			let res = await post('/monitor/rt/findAllDriverList');
			let data = res.data.data;
			let address = await getAddress(data.map(v=>[v.lng,v.lat]));
			data.forEach((v,i)=>{
				if(address[i])
					v.address = address[i].formattedAddress;
			})
			dispatch('loadBottomList',{})
			commit('changeDriverList',data);
			commit('changeIsDriverLoading',false);
		},
		async loadProvinceList({commit,dispatch}){
			let res = await post('/common/findAllProvince');
			commit('changeProvinceList',res.data.data);
		},
		async loadCityList({commit,dispatch},pid){
			let res = await post('/common/findCityListByPid',{pid});
			commit('changeCityList',res.data.data);
		},
		async loadBottomList({commit,dispatch}){
			commit('changelistLoading',true);
			// let res1 = await post('/monitor/rt/findDriverRunListByPage');
			let res2 = await post('/monitor/rt/findOverSpeedAlarmList');
			let address2 = await getAddress(res2.data.data.map(v=>[v.startLng,v.startLat]));
			res2.data.data.forEach((v,i)=>{
				if(address2[i])
					v.address = address2[i].formattedAddress;
			})
			let res3 = await post('/monitor/rt/findTiredDriveAlarmList');
			let address3 = await getAddress(res3.data.data.map(v=>[v.startLng,v.startLat]));
			res3.data.data.forEach((v,i)=>{
				if(address3[i])
					v.address = address3[i].formattedAddress;
			})
			let res4 = await post('/monitor/rt/findThreeAlarmList');
			let address4 = await getAddress(res4.data.data.map(v=>[v.startLng,v.startLat]));
			res4.data.data.forEach((v,i)=>{
				if(address4[i])
					v.address = address4[i].formattedAddress;
			})

			commit('changeoverSpeedList',res2.data.data);
			commit('changeplList',res3.data.data);
			commit('changealarmList',res4.data.data);
			commit('changelistLoading',false);
		},
	},
	subscriptions({dispatch},router){
		//给路由加监听一旦进入module绑定的页面时dispatch初始化action
		router.afterEach((to,from)=>{
			if(to.name === this.namespace){
				dispatch('loadDriverList',{});
				dispatch('loadProvinceList',{})
				
			}
		})
	}
}
